package zj.com.activities;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import zj.com.adapter.DqrSggxAdapter;
import zj.com.model.DjcModel;
import zj.com.model.Gjgx;
import zj.com.model.JcxDB;
import zj.com.net.NetManager;
import zj.com.net.OnResponseListener;
import zj.com.util.DialogUtil;
import zj.com.util.LogUtils;
import zj.com.util.SharedPreferencesUtil;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/8.
 * 待确认施工工序 验收
 */
public class DqrSggxActivity extends BaseActivity implements View.OnClickListener{

    public TextView title_name,right_tv,cancel_tv,submit_tv,del_tv;
    public ImageView left_iv;
    public ListView listView;
    public DqrSggxAdapter adapter;
    public List<Gjgx> list;

    public Intent intent;

    public LinearLayout bottom_ll;
    public TextView add_tc;
    public RelativeLayout error_rl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dqr_sggx);
        initView();
        initData();
    }

    public void initView(){


        error_rl = (RelativeLayout) findViewById(R.id.error_rl);
        del_tv = (TextView) findViewById(R.id.del_tv);
        del_tv.setOnClickListener(this);
        del_tv.setVisibility(View.GONE);
        cancel_tv = (TextView) findViewById(R.id.cancel_tv);
        cancel_tv.setOnClickListener(this);
        bottom_ll = (LinearLayout) findViewById(R.id.bottom_ll);
        submit_tv = (TextView) findViewById(R.id.submit_tv);
        submit_tv.setOnClickListener(this);
        submit_tv.setText("确认");

        title_name = (TextView) findViewById(R.id.title_name);
        title_name.setText("关键工序确认");
        right_tv = (TextView) findViewById(R.id.right_tv);
        right_tv.setText("编辑");
        right_tv.setOnClickListener(this);
        left_iv = (ImageView) findViewById(R.id.left_iv);
        left_iv.setOnClickListener(this);
        left_iv.setVisibility(View.VISIBLE);
        listView = (ListView) findViewById(R.id.listView);

        add_tc = (TextView) findViewById(R.id.add_tc);
        add_tc.setOnClickListener(this);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                intent = new Intent(DqrSggxActivity.this,DqrSggxEditActivity.class);
                intent.putExtra("gjgx",list.get(position));
                startActivity(intent);
            }
        });

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {


               // adapter.setSelect(list.get(position));
                bottom_ll.setVisibility(View.VISIBLE);
                right_tv.setVisibility(View.GONE);
                adapter.setIsEdit(true);

                return false;
            }
        });
    }

    public void initData(){

        list = new ArrayList<Gjgx>();


        adapter = new DqrSggxAdapter(list,this);
        listView.setAdapter(adapter);


    }

    @Override
    protected void onResume() {
        obtainData();
        super.onResume();
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.left_iv:
                finish();
                break;
            case R.id.cancel_tv:
                bottom_ll.setVisibility(View.GONE);
                right_tv.setVisibility(View.VISIBLE);
                adapter.setIsEdit(false);
                adapter.clearSelectMap();

                break;
            case R.id.submit_tv:
                bottom_ll.setVisibility(View.GONE);
                right_tv.setVisibility(View.VISIBLE);
                adapter.setIsEdit(false);
                qr();
                break;

            case R.id.right_tv:
                bottom_ll.setVisibility(View.VISIBLE);
                right_tv.setVisibility(View.GONE);
                adapter.setIsEdit(true);

                break;
            case R.id.del_tv:
                bottom_ll.setVisibility(View.GONE);
                right_tv.setVisibility(View.VISIBLE);
                adapter.setIsEdit(false);
                break;
            case R.id.add_tc:

                intent = new Intent(DqrSggxActivity.this,ZlzpActivity.class);
                startActivity(intent);
                break;
        }
    }


    public int totalCount = 0;
    public void qr(){


      Map<String,Gjgx> selectMap =   adapter.getSelectMap();
        totalCount = selectMap.size();
        for(Map.Entry<String, Gjgx> entry:selectMap.entrySet()) {

            Gjgx gjgx = selectMap.get(entry.getKey());
            doSure(gjgx);
            LogUtils.getInstance().error("====="+gjgx.getName());
        }
        adapter.clearSelectMap();

    }

    public ProgressDialog dialog;
    public void obtainData(){

        dialog = DialogUtil.createProgressDialog(this,"提示","数据获取中...");
        NetManager.getInstance(this).GetToBeConfirmedCriticalActivity("1", "15", new OnResponseListener<List<Gjgx>>() {
            @Override
            public void onComplete(boolean isOK, List<Gjgx> result, int errorCode, String errorMsg) {

                if(isOK)
                {

                    list = result;
                    if(list != null && list.size() > 0){
                        error_rl.setVisibility(View.GONE);
                    }else{
                        error_rl.setVisibility(View.VISIBLE);

                    }
                    adapter.setDataChange(result);
                }else{
                    error_rl.setVisibility(View.VISIBLE);

                    Toast.makeText(DqrSggxActivity.this, "获取失败", Toast.LENGTH_SHORT).show();

                }
                dialog.dismiss();
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                error_rl.setVisibility(View.VISIBLE);

                Toast.makeText(DqrSggxActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });
    }



    public ProgressDialog dialog2;
    public int count = 0;
    public  void doSure(Gjgx gjgx){
        String uid = (String) SharedPreferencesUtil.getData(this, "uid", "");
        dialog2 = DialogUtil.createProgressDialog(this,"提示","处理中..");
        String request = "";
        JSONObject obj = new JSONObject();
        try
        {
            obj.put("UserID",uid);
           obj.put("ProcessId",gjgx.getID());
            obj.put("Remark","");
        }catch(Exception e){

        }
        NetManager.getInstance(this).Confirm(obj.toString(), new OnResponseListener<List<JcxDB>>() {
            @Override
            public void onComplete(boolean isOK, List<JcxDB> result, int errorCode, String errorMsg) {
                adapter.clearSelectMap();

                if(isOK){
                    count ++;
                    if(count == totalCount){
                        dialog2.dismiss();
                        obtainData();
                    }
                    Toast.makeText(DqrSggxActivity.this, "处理成功", Toast.LENGTH_SHORT).show();


                }else{
                    Toast.makeText(DqrSggxActivity.this, "处理失败", Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                dialog2.dismiss();
                Toast.makeText(DqrSggxActivity.this, "处理失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

}
